package com.team.game.Game.Wizards.FireWizard;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.team.game.Game.Impacts;
import com.team.game.Game.Levels;
import com.team.game.framework.Abstract.GesturesInfo;
import com.team.game.framework.Abstract.Spell;
import com.team.game.framework.Game;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;
import com.team.game.framework.impl.SurfaceThread;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Scale_Y;
import static com.team.game.framework.impl.Scale_X_Y.relation;

public class Fire_Ray extends Spell {

    private RectF wizard;


    private Bitmap ray;
    private int i = 0;
    private PointF pointF;
    RectF posOfBmp = new RectF ();
    PointF pnt = new PointF ();
    private Matrix matrix = new Matrix ();

    public Fire_Ray () {
        super ();

        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/hero_1/spell_3.png");
            icon_spell = BitmapFactory.decodeStream (inputStream);
        } catch (IOException e) {
            e.printStackTrace ();
        }

        cost = new int[]{15, 25, 35, 45};
    }

    public Fire_Ray (Game game, RectF wizard) {
        super (game);
        this.wizard = wizard;

        TimeRecharch = 7f;
        ProgressBarRect.set(19 * Scale_X_Y.scale, 229 * Scale_X_Y.scale, 69 * Scale_X_Y.scale, 279 * Scale_X_Y.scale);
        RectDstForBlackProgress.set(21 * Scale_X_Y.scale, 231 * Scale_X_Y.scale, 67 * Scale_X_Y.scale, 277 * Scale_X_Y.scale);
        TopOfRectProgressDst = RectDstForBlackProgress.top;




        posOfBmp.set (wizard.centerX () - ray.getWidth () / 2 * Scale_X,
                wizard.centerY () - ray.getHeight () * Scale_Y,
                wizard.centerX () + ray.getWidth () / 2 * Scale_X,
                wizard.centerY ());
        pnt.set (posOfBmp.centerX (), posOfBmp.bottom);
        pointF = new PointF ();
    }

    private float timeSkill = 0;
    @Override
    public boolean updateSpell () {
        if (isDown) {
            if (timeSkill < 5) {
                if (pointF.y < wizard.centerY()) {
                    float difX = pointF.x - wizard.centerX();
                    float difY = pointF.y - wizard.centerY();

                    matrix.reset();
                    matrix.preScale(Scale_X, Scale_Y);

                    matrix.postTranslate(wizard.centerX() - ray.getWidth() / 2 * Scale_X,
                            wizard.centerY() - ray.getHeight() * Scale_Y);

                    matrix.postRotate(alpha(difX, difY), wizard.centerX(), wizard.centerY());

                    //Log.i ("Alpha: ", "" + String.valueOf (alpha (difX, difY)));

                    //game.getCurrentScreen ().getLevel ().impactOnTheRadius (,10 * i/1500 , );
                    //game.getCurrentScreen().getLevel().impactOnDegRect(posOfBmp, alpha(difX, difY), pnt, 10);
                    Impacts.impactOnDegRect (level, posOfBmp, alpha (difX, difY), pnt, 10);
                }
                timeSkill += SurfaceThread.deltaTime;
            } else
                time += SurfaceThread.deltaTime;
        }
        if (time > TimeRecharch) {
            this.restart();
            return false;
        }

        return true;
    }

    public float alpha(float difX, float difY) {
        double distance = Math.sqrt (Math.pow ((difX), 2.0) + Math.pow (difY, 2.0));

        return (float) ((Math.asin (difX / distance)) / (Math.PI) * 180);
    }

    @Override
    public boolean activeSpell (GesturesInfo gesturesInfo1) {
        Active = true;
        level = (Levels) game.getCurrentScreen ().getLevel ();
        return true;
    }

    boolean isDown = false;
    @Override
    public boolean setMotionEventSpell (MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
            isDown = true;
        if (isDown)
            pointF.set(motionEvent.getX(), motionEvent.getY());
        if (timeSkill > 5) {
            WaitForTap = false;
            return false;
        }
        return true;
    }

    Paint paint = new Paint ();

    @Override
    public void drawSpell (Canvas canvas) {
        if (isDown)
            if (timeSkill < 5)
        canvas.drawBitmap (ray, matrix, null);

    }

    @Override
    public void restart () {
        this.progressBarRestart();
        timeSkill = 0;
        isDown = false;
        WaitForTap = true;
    }

    @Override
    public void dispose () {

    }

    @Override
    public void setImages () {
        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Flame/Flame_Thrower_1.png");
            ray = BitmapFactory.decodeStream (inputStream);

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Flame/Fire_Ray_Normal.png");
            ProgressBarNormal = (BitmapFactory.decodeStream (inputStream));

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Flame/Fire_Ray_Active.png");
            ProgressBarActive = (BitmapFactory.decodeStream (inputStream));

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Flame/Fire_Ray_RActive.png");
            ProgressBarRActive = (BitmapFactory.decodeStream (inputStream));

        } catch (IOException e) {
            e.printStackTrace ();
        }

        SPELL_NAME = "fire_ray";
    }

    @Override
    public void drowProfileExample (Canvas canvas, int A) {
        paintText.setTextSize (30 * relation);
        paintText.setColor (Color.RED);
        canvas.drawText ("Working on it\n\n\n\nDamage:?/?/?/?", 660 * Scale_X, 900 * Scale_Y, paintText);
    }

    @Override
    public void updateProfileExample() {

    }

    @Override
    public void setStartConfiguration() {

    }
}
